package cn.xuexiyuan.kpi.controller;

import cn.xuexiyuan.kpi.entity.PageParam;
import cn.xuexiyuan.kpi.entity.ResponseEntity;
import cn.xuexiyuan.kpi.entity.ScoreEntity;
import cn.xuexiyuan.kpi.mapper.ScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/score")
public class ScoreController {

    @Autowired
    ScoreMapper scoreMapper;

    @RequestMapping("/save")
    public ResponseEntity save(@RequestBody ScoreEntity scoreEntity) {
        if(scoreEntity.getId() != null){
            scoreMapper.delete(scoreEntity);
        }
        scoreMapper.insert(scoreEntity);
        return ResponseEntity.buildSucess("保存成功");
    }

    @RequestMapping("/find")
    public ResponseEntity find(@RequestBody PageParam<ScoreEntity> param){
        List<ScoreEntity> data = scoreMapper.find(param);
        int count = scoreMapper.count(param);
        return ResponseEntity.buildSucess(data, count);
    }

    @RequestMapping("/summary")
    public ResponseEntity summary(@RequestBody PageParam<ScoreEntity> param){
        List<ScoreEntity> data = scoreMapper.summary(param);
        int count = scoreMapper.countSummary(param);
        return ResponseEntity.buildSucess(data, count);
    }

    @RequestMapping("/delete")
    public ResponseEntity delete(@RequestBody ScoreEntity param){
        scoreMapper.delete(param);
        return ResponseEntity.buildSucess(null, "删除成功", null);
    }
}
